﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Cloud.Test.Main.Utils
{
    public static class UpdateUtil
    {
        #region API函数声明-必须放在类中
        [DllImport("kernel32")]//返回0表示失败，非0为成功
        private static extern long WritePrivateProfileString(string section, string key,
          string val, string filePath);
        [DllImport("kernel32")]//返回取得字符串缓冲区的长度
        private static extern long GetPrivateProfileString(string section, string key,
          string def, StringBuilder retVal, int size, string filePath);
        #endregion

        public static string Serialize(object obj)
        {
            if (obj == null) return string.Empty;
            var json = JsonConvert.SerializeObject(obj);
            var bytes = Encoding.Default.GetBytes(json);
            var base64str = Convert.ToBase64String(bytes);
            return base64str;
        }

        public static T Deserialize<T>(string str)
        {
            var obj = default(T);
            if (string.IsNullOrEmpty(str)) return obj;
            try
            {
                byte[] bytes = Convert.FromBase64String(str);
                var json = Encoding.Default.GetString(bytes);
                var result = JsonConvert.DeserializeObject<T>(json);
                return result;

            }
            catch (Exception)
            {
                return default;
            }

        }
        #region 写Ini文件
        /// <summary>
        /// 将内容写入指定的ini文件中
        /// </summary>
        /// <param name="Section">ini文件中的节名</param>
        /// <param name="Key">ini文件中的键</param>
        /// <param name="Value">要写入该键所对应的值</param>
        /// <param name="iniFilePath">ini文件路径</param>
        /// <returns></returns>
        public static bool Write(string Section, string Key, string Value, string iniFilePath)
        {

            if (!File.Exists(iniFilePath))
            {
                try
                {
                    FileStream stream = File.Open(iniFilePath, FileMode.Create, FileAccess.Write);
                    //stream.Seek(0, SeekOrigin.Begin);
                    //stream.SetLength(0); //清空txt文件
                    stream.Dispose();
                    stream.Close();
                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex);
                }

            }

            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }

        }
        #endregion
        /// <summary>
        /// 读取ini文件内容的方法
        /// </summary>
        /// <param name="Section">ini文件的节名</param>
        /// <param name="Key">ini文件对应节下的健名</param>
        /// <param name="NoText">ini文件对应节对应健下无内容时返回的值</param>
        /// <param name="iniFilePath">该ini文件的路径</param>
        /// <returns></returns>
        public static string Read(string Section, string Key, string NoText, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(1024);
                GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                return temp.ToString();
            }
            else
            {
                return null;
            }
        }
    }

}
